package com.copractice.beans.categories;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.copractice.beans.BaseBean;
import com.copractice.beans.common.Constants;
import com.copractice.beans.common.MessagesUtil;
import com.copractice.bl.BLFactory;
import com.copractice.bl.tags.TagsBL;
import com.copractice.bl.tags.TagsInfo;
import com.copractice.bl.user.UserKnowledgeBL;
import com.copractice.intel.IntelQuestion;

public class CategoriesBean extends BaseBean {
    
    private static final Logger LOG = LoggerFactory.getLogger(CategoriesBean.class);
    
    /**
     * Returns the list of areas and all info about it (name, amount of questions, learning score, teaching score) 
     * for a user which id is in a session. If user id is null than empty list will be returned.
     * 
     * @return list of areas and all info about it for a user which id is in a session.
     */
    public List<CategoryVO> getAreasList() {

        if (LOG.isDebugEnabled()) {
            LOG.debug("Getting list of areas for user: Username = {}; ID = {}", 
                      getSessionAttribute(Constants.USER_NAME), getSessionAttribute(Constants.USER_ID));
        }
        
        TagsBL tagsBL = BLFactory.getInstance(TagsBL.class);
        List<CategoryVO> areasList = new ArrayList<CategoryVO>();

        if (getSessionAttribute(Constants.USER_ID) != null) {
            List<TagsInfo> tagsInfoList = tagsBL.getAreaInfo((Long) getSessionAttribute(Constants.USER_ID));

            if (LOG.isDebugEnabled()) {
                LOG.debug("Areas for userID: {}, areas: {} ", getSessionAttribute(Constants.USER_ID), tagsInfoList);
            }

            for (TagsInfo tagInfo : tagsInfoList) {
                areasList.add(new CategoryVO(tagInfo.getId(), tagInfo.getName(), tagInfo.getAmountOfQuestions(), 
                                             tagInfo.getLearningScore(), tagInfo.getTeachingScore()));
            }
        }

        return areasList;
    }
    
    /**
     * Loads practice page in the selected area 
     * 
     * @return
     */
    public String loadPracticePage() {
        boolean a = true;
        if (a) {
            return "success";
        }
        
        if (LOG.isDebugEnabled()) {
            LOG.debug("Loading practice page for user = '{}'", getSessionAttribute(Constants.USER_ID));
        }

        Map<String, String> paramMap = getRequestParameterMap();
        
        String catStrId = (String) paramMap.get("id");
        if (LOG.isDebugEnabled()) {
            LOG.debug("User: '{}' selected category: '{}'", getSessionAttribute(Constants.USER_ID), catStrId);
        }

        Long categoryId = null;
        if (catStrId != null) {
            try {
                categoryId = Long.valueOf(catStrId);
            } catch (NumberFormatException e) {
                LOG.error(e.getMessage());
            }
        }
        initializePracticeParameters();
        if (categoryId != null) {
            UserKnowledgeBL userKnowledgeBL = BLFactory.getInstance(UserKnowledgeBL.class);
            userKnowledgeBL.createUsersKnowledgeIfNecessary((Long)getSessionAttribute(Constants.USER_ID), categoryId);
            
            IntelQuestion intelQuestion = BLFactory.getInstance(IntelQuestion.class);
            Long nextQuestionId = intelQuestion.getNextQuestion((Long)getSessionAttribute(Constants.USER_ID), 
                                                                categoryId); 
            if (nextQuestionId == null || nextQuestionId < 0) {
                MessagesUtil.addMessage(null, Constants.MESSAGE_BUNDLE, "practice.no_more_questions");
                return "fail";
            }
            
            setSessionAttribute(Constants.CATEGORY_ID, categoryId);
            if (LOG.isDebugEnabled()) {
                LOG.debug("'success' will be returned");
            }
            return "success";
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("'fail' will be returned");
            }
            MessagesUtil.addMessage(null, Constants.MESSAGE_BUNDLE, "practice.error");
            //TODO
            return "fail";
        }
    }    
    
    private void initializePracticeParameters() {
        setSessionAttribute(Constants.FEEDBACKS_TO_DECREASE_RATING, null);
        setSessionAttribute(Constants.FEEDBACKS_TO_INCREASE_RATING, null);
        setSessionAttribute(Constants.PREVIOUS_ANSWER_ID, null);
    }
    
    

}
